﻿using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing.Fields;
using Microsoft.SharePoint.Linq;

namespace $rootnamespace$
{
    /// <summary>
    /// Partial class for extending SPMetal created by Paul Beck 29/11/2010
    /// $safeitemrootname$
    /// </summary>
	public partial class $safeitemrootname$
	{
        #region "Attachments"

        private const string CONTENT_FIELD = "Attachments";

        private SPAttachmentCollection _contentAttachments;
        public SPAttachmentCollection ContentAttachments
        {
            get
            {
                return _contentAttachments;
            }
            set
            {
                if (value != _contentAttachments)
                {
                    this.OnPropertyChanging(CONTENT_FIELD, _contentAttachments);
                    _contentAttachments = value;
                    this.OnPropertyChanged(CONTENT_FIELD);
                }
            }
        }

        #endregion

        #region "PublishImage"

        private const string CONTENT_FIELD_IMG = "MyPublishingImage";

        private ImageFieldValue _contentMyPublishingImage;
        public ImageFieldValue ContentMyPublishingImage
        {
            get
            {
                return _contentMyPublishingImage;
            }
            set
            {
                if (value != _contentMyPublishingImage)
                {
                    this.OnPropertyChanging(CONTENT_FIELD, _contentAttachments);
                    _contentMyPublishingImage = value;
                    this.OnPropertyChanged(CONTENT_FIELD);
                }
            }
        }

        #endregion

        #region "PublishHTML"

        private const string CONTENT_FIELD_HTML = "PublishingHtml";

        private HtmlField _contentMyPublishingHtml;
        public HtmlField ContentMyPublishingHtml
        {
            get
            {
                return _contentMyPublishingHtml;
            }
            set
            {
                if (value != _contentMyPublishingHtml)
                {
                    this.OnPropertyChanging(CONTENT_FIELD_HTML, _contentMyPublishingHtml);
                    _contentMyPublishingHtml = value;
                    this.OnPropertyChanged(CONTENT_FIELD_HTML);
                }
            }
        }

        #endregion

        #region "CreatedBy (Hidden field)"

        string _CreatedBy;

        [Microsoft.SharePoint.Linq.ColumnAttribute(Name = "Author", Storage = "_CreatedBy", ReadOnly = true, FieldType = "User", IsLookupValue = true)]
        public string CreatedBy
        {
            get
            {
                return this._CreatedBy;
            }
            set
            {
                if ((value != this._CreatedBy))
                {
                    this.OnPropertyChanging("CreatedBy", this._CreatedBy);
                    this._CreatedBy = value; 
                    this.OnPropertyChanged("CreatedBy");
                }
            }
        }

        #endregion

        /// <summary>
        /// Read only data is retrieved in this method for each extended SPMetal field
        /// Used to Read - CRUD operation performed by SPMetal
        /// </summary>
        /// <param name="listItem"></param>
        [CustomMapping(Columns = new string[] { CONTENT_FIELD, CONTENT_FIELD_IMG, CONTENT_FIELD_HTML })]
        public void MapFrom(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            if ((bool)item[CONTENT_FIELD] != false)
            {
                this.ContentAttachments = (SPAttachmentCollection)item.Attachments;
            }
            this.ContentMyPublishingImage = item[CONTENT_FIELD_IMG] as ImageFieldValue;
            this.ContentMyPublishingHtml = item[CONTENT_FIELD_HTML] as HtmlField;
        }

        /// <summary>
        /// Used to Insert & Update - CRUD operation performed by SPMetal
        /// </summary>
        /// <param name="listItem"></param>
        public void MapTo(object listItem)
        {
            SPListItem item = (SPListItem)listItem;
            item[CONTENT_FIELD_IMG] = this.ContentMyPublishingImage;
            item[CONTENT_FIELD_HTML] = this.ContentMyPublishingHtml;
        }

        public void Resolve(RefreshMode mode, object originalListItem, object updatedlistItem)
        {
            //SPListItem originalItem = originalListItem as SPListItem;
            //SPListItem updateItem = updatedlistItem as SPListItem;
            //ImageFieldValue originalValue = originalItem[CONTENT_FIELD_IMG] as ImageFieldValue;
            //ImageFieldValue updateValue = updateItem[CONTENT_FIELD_IMG] as ImageFieldValue;
            //switch (mode)
            //{
            //    case RefreshMode.OverwriteCurrentValues:
            //        this.ContentMyPublishingImage = updateValue;
            //        break;
            //    case RefreshMode.KeepCurrentValues:
            //        updateItem[CONTENT_FIELD_IMG] = this.ContentMyPublishingImage;
            //        break;
            //    case RefreshMode.KeepChanges:
            //        if (this.ContentMyPublishingImage != originalValue)
            //            updateItem[CONTENT_FIELD_IMG] = this.ContentMyPublishingImage;
            //        else if (this.ContentMyPublishingImage == originalValue && this.ContentMyPublishingImage != updateValue)
            //            this.ContentMyPublishingImage = updateValue;
            //        break;
            //}

        }


	}
}
